package com.atguigu.activemq.queue;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * @Author luliang
 * @Date 2019-12-02 17:15
 */
public class JmsProduce {
    public static final String ACTIVEMQ_URL = "tcp://luliang888.top:61616";
    public static final String QNEUE_NAME = "queue01";
    public static void main(String[] args) throws Exception{
        //1.创建连接工厂
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);
        //2.通过连接工厂，获得连接的connection，并启动访问
        Connection connection = activeMQConnectionFactory.createConnection();
        connection.start();
        //3.创建回话Session
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //4.创建目的地，是quene还是topic
        Queue queue = session.createQueue(QNEUE_NAME);
        //5.创建消息的生产者
        MessageProducer producer = session.createProducer(queue);
        //6.通过MessageProducer将生产的消息发送到队列里面
        for (int i = 1; i <= 5; i++) {
            //7.创建消息，通过messageProducer发送给mq
            TextMessage textMessage = session.createTextMessage("msg - " + i);
            producer.send(textMessage);
        }
        //8。关闭资源
        producer.close();
        session.close();
        connection.close();
        System.out.println("消息发送成功。。。");
    }
}
